<?php

namespace app\common\model\Team;

// 引入TP基础模型
use think\Model;

// 拼团订单模型
class  Team extends Model
{
    protected $name = 'team_order';

    protected $autoWriteTimestamp = true;

    protected $createTime = 'createtime';

    protected $updateTime = false;

    protected $append = [
        'endtime_text', // 结束时间
        'buy_text', //拼团价格
    ];

    public function getEndtimeTextAttr($value, $data)
    {
        $endtime = !empty($data['endtime']) ? $data['endtime'] : '';

        if ($endtime)
        {
            return   date("Y-m-d", $endtime);
        }
        else
        {
            return '';
        }
    }

    public function getBuyTextAttr($value, $data)
    {
        $teamid = !empty($data['id']) ? $data['id'] : 0;

        $price = 0;

        if($teamid)
        {
            $name = model('Team.OrderBusiness')::where('teamid', $teamid)->value('price');
            $price = $name;
        }

        return $price;
    }

    public function product()
    {
        return $this->belongsTo('app\common\model\Product\Product', 'proid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
